package _01_动态数组;

public class _495_提莫攻击 {

    public static void main(String[] args) {

        _495_提莫攻击 v = new _495_提莫攻击();

        int[] ins = {1, 4};
        System.out.println(v.findPoisonedDuration(ins, 2));

    }

    public int findPoisonedDuration(int[] timeSeries, int duration) {
        int total = 0;
        int begin = timeSeries[0];
        int end = timeSeries.length;
        for (int i = 0; i < end - 1; i++) {
            if (timeSeries[i] + duration < timeSeries[i + 1]) {
                // 无交集
                total += timeSeries[i] + duration - begin;
                begin = timeSeries[i + 1];
            }
        }
        // 计算最终结果
        total += timeSeries[end - 1] + duration - begin;
        return total;
    }

}
